﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Data.aDbDataSetTableAdapters;

namespace AtletIS.Shared.Forms.Signups
{
    public partial class frmSignupsQuick : Form
    {

        #region Form

        // Constructor
        public frmSignupsQuick()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.AtletIS64;
        }

        // Form Load
        private void frmSignupsQuick_Load(object sender, EventArgs e)
        {
            // Fill Countries combo
            this.countriesTableAdapter.Fill(this.aDbDataSet.Countries);
            this.countriesComboBox.SelectedValue = "SVK";

            this.lblTitle.Text = Globals.ActiveCompetition.Name;
            
            Helpers.DisciplineListHelper.FillTreeView(tvDisciplines);
            txtSurname.Focus();
        }

        #endregion

        #region Button events

        private void btnSignup_Click(object sender, EventArgs e)
        {
            if (txtSurname.Text == string.Empty)
            {
                MessageBox.Show("Zadajte priezvisko, prosím", "Prihlášky", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (txtFirstname.Text == string.Empty)
            {
                MessageBox.Show("Zadajte meno, prosím", "Prihlášky", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (txtBirthdate.Text.Length != 8)
            {
                MessageBox.Show("Zadajte dátum narodenia vo formáte 'DDMMRRRR', prosím", "Prihlášky", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            bool nodeSelected = false;
            foreach (TreeNode tn in tvDisciplines.Nodes)
            {
                if (tn.Checked)
                {
                    nodeSelected = true;
                    break;
                }

            }

            if (nodeSelected == false)
            {
                MessageBox.Show("Vyberte disciplínu, prosím", "Prihlášky", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            PerformancesTableAdapter taPerformances = new PerformancesTableAdapter();
            int SB = 0, stNum = 0, count = 0;
            DateTime birthdate;
            
            try
            {
                foreach (TreeNode tn in tvDisciplines.Nodes)
                {
                    if (tn.Checked)
                    {
                        try
                        {
                            birthdate = new DateTime(Convert.ToInt32(txtBirthdate.Text.Substring(4, 4)), Convert.ToInt32(txtBirthdate.Text.Substring(2, 2)), Convert.ToInt32(txtBirthdate.Text.Substring(0, 2)));
                        }
                        catch
                        {
                            birthdate = new DateTime();
                        }

                        if (txtSB.Text != string.Empty)
                            SB = Convert.ToInt32(txtSB.Text);

                        if (txtStNum.Text != string.Empty)
                            stNum = Convert.ToInt32(txtStNum.Text);

                        #region Prihlasenie pretekara

                        AtletIS.Shared.Data.aDbDataSet.PerformancesRow drPerformance = aDbDataSet.Performances.NewPerformancesRow();

                        drPerformance.DisciplineListId = Convert.ToInt32(tn.Name);
                        drPerformance.CompetitorId = 1; // FIKT
                        drPerformance.FirstName = this.txtFirstname.Text;
                        drPerformance.Surname = this.txtSurname.Text;
                        drPerformance.Birthdate = birthdate;
                        drPerformance.Club = txtClub.Text;
                        drPerformance.CountryId = countriesComboBox.SelectedValue.ToString();
                        drPerformance.SMS = string.Empty;
                        drPerformance.StartNumber = stNum;
                        drPerformance.Lane = 0;
                        drPerformance.Code = 0;
                        drPerformance.SB = SB;   // Lokalne prihlaseni pretekari nemaju SB, PB zo statistiky
                        drPerformance.PB = SB;
                        drPerformance.Result = 0;
                        drPerformance.ResultQuality = string.Empty;
                        drPerformance.AdvResult = 0;
                        drPerformance.Rank = 0;
                        drPerformance.SecondaryRank = 0;
                        drPerformance.Reaction = string.Empty;
                        drPerformance.Points = 0;
                        drPerformance.PointsScoring = true;
                        drPerformance.Active = true;
                        drPerformance.Sync = false;
                        drPerformance.WebId = 0;
                        drPerformance.Modified = DateTime.Now;
                        drPerformance.SetRealStartTimeNull();

                        this.aDbDataSet.Performances.AddPerformancesRow(drPerformance);
                        taPerformances.Update(drPerformance);

                        #endregion

                        count += 1;
                    }
                }

                this.tsslSignup.Text = string.Format("[{0}] {1} {2} bol prihlásený ({3}x).", DateTime.Now.ToLongTimeString(), txtFirstname.Text, txtSurname.Text, count.ToString());

                // Clear GUI
                txtSurname.Text = string.Empty;
                txtFirstname.Text = string.Empty;
                txtBirthdate.Text = string.Empty;
                txtClub.Text = string.Empty;
                txtSB.Text = string.Empty;
                txtStNum.Text = string.Empty;
                txtSms.Text = string.Empty;

                foreach (TreeNode tn in tvDisciplines.Nodes)
                {
                    tn.Checked = false;
                }

                txtSurname.Focus();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Prihlášky", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

    }
}
